package com.service.uai;

import java.util.List;

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

import com.bpms.common.CommonUtil;
import com.bpms.common.VO;
import com.bpms.esb.IService;

public class Buildhdqueryxml implements IService {

	private Document doc = null;
	private Element root = null;

	/**
	 * add an element into xml (use jDom)
	 */
	public void addElem(String path, String value) {
		String[] paths = path.split("/");
		if (doc == null)
			doc = new Document();
		if (root == null) {
			root = new Element(paths[0]);
			doc.addContent(root);
		}
		Element temp = root;
		boolean b = false;
		int i = 1;
		for (i = 1; i < paths.length; i++) {
			List ls = temp.getChildren(paths[i]);
			if (ls.size() <= 0) {
				b = true;
				break;
			}
			temp = (Element) ls.get(0);
		}

		if (b) {
			for (; i < paths.length; i++) {
				temp.addContent(new Element(paths[i]));
				temp = (Element) temp.getChildren(paths[i]).get(0);
			}
		}
		temp.setText(value);

	}

	/**
	 * build the xml (use jDom)
	 * 
	 * @return
	 */
	public String sendXML() {
		Format format = Format.getCompactFormat();
		format.setEncoding("GB2312");
		XMLOutputter out = new XMLOutputter(format);
		format.setLineSeparator("");
		out.setFormat(format);
		String xmlBody = out.outputString(doc).trim();

		return xmlBody;
	}

	/**
	 * 支付密码检验
	 * 
	 * 
	 * instanceid: 流水号
	 * 
	 * PWDBIZTYPE：凭证类型
	 * 
	 * accountno：32位账号
	 * 
	 * password：16位密码
	 * 
	 * signdate：8位签发日期
	 * 
	 * money：20位金额
	 * 
	 * biznum：32位凭证号
	 * 
	 * @return,CONDITION(Y,N,E)
	 */
	public VO exec(VO vo) {
		VO retvo = new VO();
		try {
			addElem("ebank/gDecl/cMsgType", "1");
			addElem("ebank/gTxData/cHostCod", vo.getAttr("cHostCod"));
			addElem("ebank/gTxData/cGoalId", vo.getAttr("cGoalId"));
			addElem("ebank/gTxData/cReqId", vo.getAttr("cReqId"));
			addElem("ebank/gTxData/cChannel", vo.getAttr("cChannel"));
			addElem("ebank/gTxData/dReqDt", vo.getAttr("dReqDt"));
			addElem("ebank/gTxData/tReqTm", vo.getAttr("tReqTm"));
			addElem("ebank/gTxData/iReqTrace", vo.getAttr("iReqTrace"));
			addElem("ebank/gTxData/iHstCstId", vo.getAttr("iHstCstId"));
			addElem("ebank/gTxData/accNo", vo.getAttr("accNo"));
			addElem("ebank/gTxData/backStartDate", vo.getAttr("backStartDate"));
			addElem("ebank/gTxData/backEndDate", vo.getAttr("backEndDate"));
			addElem("ebank/gTxData/backStt", vo.getAttr("backStt"));
			addElem("ebank/gTxData/tranSum", vo.getAttr("tranSum"));
			addElem("ebank/gTxData/otherAccNo", vo.getAttr("otherAccNo"));
			addElem("ebank/gTxData/oriBackNo", vo.getAttr("oriBackNo"));
			retvo.setAttr("message", sendXML());
			return retvo;
		} catch (Exception e) {
			CommonUtil.debug("uai.Buildhdqueryxml exception", e);
			return null;
		}

	}

}
